package com.pattern.creational_pattern.singleton_pattern;


/**
 * @Description
 * @Author Ping
 * @Date 2022/3/20 23:28
 **/
public class Singleton {
    /**
     * a eager way to provide the single object.
     */
    private static Singleton eagerSingle = new Singleton();
    /**
     * lazy way to provide the single;
     * use volatile to assure each copy of lazySingle in each threads in consistent.
     */
    private static volatile Singleton lazySingle ;
    private Singleton() {

    }

    /**
     * get the single object by two methods .
     * Wherein, eager method and lazy method.
     *
     * @return
     */
    public static Singleton getSingle() {

        return eagerSingle ;
    }

    public static Singleton getSingleByLazy() {
        // Critical Area (CA) : more precise, much better
        synchronized (Singleton.class) {
            if (lazySingle == null) {
                // time expensive for create the singleton, normally.
                lazySingle = new Singleton();
            }
        }
        return lazySingle ;
    }

}
